package dao

import (
	"week32/inits"
	"week32/model"
)

func Login(tel string, password string) (result *model.User) {
	err := inits.DB.Where("tel=?", tel).Where("password=?", password).Find(&result).Error
	if err != nil {
		return nil
	}
	return result
}

func Register(tel string, password string) (result *model.User) {
	result = &model.User{
		Tel:      tel,
		Password: password,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return nil
	}
	return result
}

func List() (user *model.User) {
	err := inits.DB.Find(&user).Error
	if err != nil {
		return nil
	}
	return user
}

func CreateSend(formUserId int, toUserId int, message string) bool {
	var min1 = formUserId
	var max1 = toUserId
	if formUserId > toUserId {
		min1 = toUserId
		max1 = formUserId
	}
	result := &model.Message{
		FormUserId: formUserId,
		ToUserId:   toUserId,
		Message:    message,
		RoomId:     min1*100000 + max1,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return false
	}
	return true
}

func ChatList() (result *model.Message) {
	err := inits.DB.Where("room_id=?", 100002).Find(&result).Error
	if err != nil {
		return nil
	}
	return result
}
